﻿using System.Web.Http;
using System.Web.Mvc;
using EasyGo.Data;
using EasyGo.Data.Dao;
using EasyGo.Data.Dao.Impl;
using EasyGo.Services;
using EasyGo.Services.Impl;
using EasyGo.Web.Authentication;
using EasyGo.Web.Authentication.Impl;
using EasyGo.Web.Ioc;
using StructureMap;

namespace EasyGo.Web.Config
{
    public static class Ioc
    {
        public static void Init()
        {
            ObjectFactory.Initialize(x =>
            {
                x.Scan(scan =>
                {
                    scan.TheCallingAssembly();
                    scan.WithDefaultConventions();
                });

                x.Scan(scan =>
                {
                    scan.AssemblyContainingType<Service>();
                    scan.IncludeNamespaceContainingType<CompanyService>();
                    scan.RegisterConcreteTypesAgainstTheFirstInterface();
                });

                x.Scan(scan =>
                {
                    scan.AssemblyContainingType<SqlDao>();
                    scan.IncludeNamespaceContainingType<CompanyDao>();
                    scan.RegisterConcreteTypesAgainstTheFirstInterface();
                });

                x.For<EasyGoDbContext>().HttpContextScoped().Use<EasyGoDbContext>();
                x.For<IUnitOfWork>().HttpContextScoped().Use<SqlUnitOfWork>();
            });
            DependencyResolver.SetResolver(new EasyGoIocResolver(ObjectFactory.Container));
            GlobalConfiguration.Configuration.DependencyResolver =
                new StructureMapDependencyResolver(ObjectFactory.Container);
            ControllerBuilder.Current.SetControllerFactory(new EasyGoControllerFactory());
        }
    }
}